import 'package:intl/intl.dart';

class DateUtil {
  ///日期转时间戳
  static int dateToTimestamp(String date, {isMicroseconds = false}) {
    DateTime dateTime = DateTime.parse(date);
    int timestamp = dateTime.millisecondsSinceEpoch;
    if (isMicroseconds) {
      timestamp = dateTime.microsecondsSinceEpoch;
    }
    return timestamp;
  }

  static String getFormattedTime(int millis) {
    var now = DateTime.fromMillisecondsSinceEpoch(millis);
    var formatter = DateFormat('yyyy.MM.dd HH:mm:ss');
    return formatter.format(now);
  }
  static String getFormattedTime_(int millis) {
    var now = DateTime.fromMillisecondsSinceEpoch(millis);
    var formatter = DateFormat('yyyy-MM-dd HH:mm:ss');
    return formatter.format(now);
  }

  static String getCurrentTimeForType(int millis, String type) {
    var now = DateTime.fromMillisecondsSinceEpoch(millis);
    var formatter = DateFormat(type);
    return formatter.format(now);
  }

  static String getNowTimeStr() {
    return DateUtil.getCurrentTimeForType(
        DateTime.now().millisecondsSinceEpoch, 'yyyy-MM-dd HH:mm');
  }


  static String getNowTimeStr_() {
    return DateUtil.getCurrentTimeForType(
        DateTime.now().millisecondsSinceEpoch, 'yyyy-MM-dd HH:mm:ss');
  }



  static String getCurrentYMD() {
    return DateUtil.getCurrentTimeForType(
        DateTime.now().millisecondsSinceEpoch, 'yyyy-MM-dd');
  }

  //獲取當前時間
  static String getNowTime() {
    return DateUtil.getCurrentTimeForType(
        DateTime.now().millisecondsSinceEpoch, 'yyyy-MM-dd HH:mm');
  }

  /**
   * 参数是需要减少的偏移毫秒数
   */
  static String getDateStr(int offSub) {
    return DateUtil.getCurrentTimeForType(DateTime.now().millisecondsSinceEpoch - offSub, 'yyyy-MM-dd');
  }

  /**
   * @param dateStr 格式位 yyyy-MM-dd
   */
  //日期是否大于今天
  static bool beyondToday(String dateStr) {
    return dateStr.compareTo(getCurrentYMD()) > 0;
  }

  /**
   * @param dateStr 格式位 yyyy-MM-dd HH:mm
   */
  //日期是否大于今天(时分秒)
  static bool beyondTodayTime(String dateStr) {
    return dateStr.compareTo(getNowTimeStr()) > 0;
  }

  /**
   * @param dateStr 格式位 yyyy-MM-dd
   */
  //日期是否大于昨天
  static bool beyondYesterday(String dateStr) {
    String yesterday = getDateStr(1000 * 3600 * 24);
    return dateStr.compareTo(yesterday) > 0;
  }


  ///日期是否是今天
  static bool isToday(DateTime dateTime) {
    DateTime now = DateTime.now();
    DateTime localDateTime = dateTime.toLocal();

    if (localDateTime.day == now.day && localDateTime.month == now.month &&
        localDateTime.year == now.year) {
      return true;
    }
    return false;
  }

  ///日期是否是昨天
  static bool isYesterday(DateTime dateTime) {
    DateTime now = DateTime.now();
    DateTime localDateTime = dateTime.toLocal();
    DateTime yesterday = now.subtract(const Duration(days: 1));

    if (localDateTime.day == yesterday.day && localDateTime.month == yesterday.month &&
        localDateTime.year == yesterday.year) {
      return true;
    }
    return false;
  }
}
